package jianzhi

func maxValue(grid [][]int) int {
	m, n := len(grid), len(grid[0])

	for i, v := range grid {
		for j := range v {
			var up, left int
			if i > 0 {
				up = grid[i-1][j]
			}
			if j > 0 {
				left = grid[i][j-1]
			}

			grid[i][j] = max(up, left) + grid[i][j]
		}
	}

	return grid[m-1][n-1]
}

func max(i, j int) int {
	if i > j {
		return i
	} else {
		return j
	}
}
